package com.my.mvvmrapid.ui.main.fragment.homewan.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.my.mvvmrapid.R;
import com.my.mvvmrapid.entity.Banner;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * Created by zhongjh on 2021/4/9.
 * 玩Android首页的banner
 */
public class HomeWanBannerAdapter extends BannerAdapter<Banner, HomeWanBannerAdapter.BannerViewHolder> {

    Context mContext;

    public HomeWanBannerAdapter(List<Banner> mDatas) {
        // 设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
    }

    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        // 创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
        ImageView imageView = new ImageView(parent.getContext());
        // 注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerViewHolder(imageView);
    }


    @Override
    public void onBindView(BannerViewHolder holder, Banner data, int position, int size) {
        Glide.with(mContext).load(data.getImagePath())
                .apply(new RequestOptions().placeholder(R.drawable.placeholder_res))
                .centerCrop()
                .into(holder.imageView);
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }

}
